package cn.gcyl.module.biz.service.contactplatform;

import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.util.object.BeanUtils;
import cn.gcyl.module.biz.controller.admin.contactplatform.vo.*;
import cn.gcyl.module.biz.dal.dataobject.contactplatform.ContactPlatformDO;
import cn.gcyl.module.biz.dal.mysql.contactplatform.ContactPlatformMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.gcyl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.gcyl.module.system.enums.ErrorCodeConstants.CONTACT_PLATFORM_NOT_EXISTS;

/**
 * 联系平台 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ContactPlatformServiceImpl implements ContactPlatformService {

    @Resource
    private ContactPlatformMapper contactPlatformMapper;

    @Override
    public Long createContactPlatform(ContactPlatformSaveReqVO createReqVO) {
        // 插入
        ContactPlatformDO contactPlatform = BeanUtils.toBean(createReqVO, ContactPlatformDO.class);
        contactPlatformMapper.insert(contactPlatform);

        // 返回
        return contactPlatform.getId();
    }

    @Override
    public void updateContactPlatform(ContactPlatformSaveReqVO updateReqVO) {
        // 校验存在
        validateContactPlatformExists(updateReqVO.getId());
        // 更新
        ContactPlatformDO updateObj = BeanUtils.toBean(updateReqVO, ContactPlatformDO.class);
        contactPlatformMapper.updateById(updateObj);
    }

    @Override
    public void deleteContactPlatform(Long id) {
        // 校验存在
        validateContactPlatformExists(id);
        // 删除
        contactPlatformMapper.deleteById(id);
    }

    @Override
        public void deleteContactPlatformListByIds(List<Long> ids) {
        // 删除
        contactPlatformMapper.deleteByIds(ids);
        }


    private void validateContactPlatformExists(Long id) {
        if (contactPlatformMapper.selectById(id) == null) {
            throw exception(CONTACT_PLATFORM_NOT_EXISTS);
        }
    }

    @Override
    public ContactPlatformDO getContactPlatform(Long id) {
        return contactPlatformMapper.selectById(id);
    }

    @Override
    public PageResult<ContactPlatformDO> getContactPlatformPage(ContactPlatformPageReqVO pageReqVO) {
        return contactPlatformMapper.selectPage(pageReqVO);
    }

}